/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package explorer;

import gui.FileExplorerView;
import java.io.File;
import java.util.ArrayList;

/**
 *
 * @author thomas
 */
public class FileExplorerController {

    private static FileExplorerController __instance = null;
    
    private FileExplorerView _myView;
    private FileExplorerModel _myModel;
    
    private File _root;

    private FileExplorerController() {
        _myModel = new FileExplorerModel();
    }

    private FileExplorerController(FileExplorerView aView) {
        this();
        _myView = aView;
    }
    
    public static FileExplorerController getInstance(FileExplorerView aView) {
        if (__instance == null)
            __instance = new FileExplorerController(aView);
        return __instance;
    }

    /**
     * @return the _root
     */
    public File getRoot() {
        return _root;
    }

    /**
     * @param root the _root to set
     */
    public void setRoot(File root) {
        this._root = root;
    }
    
    /**
     * @param path the _root to set
     */
    public void setRoot(String path) {
        this._root = new File(path);
    }

    public void changeDirectory(String newDir) {
        _myModel.setPath(newDir);
        System.out.println("On change de répertoire: " + newDir);
//        _myView.changeDirectory(newDir);
    }

    /**
     * @return the _myModel
     */
    public FileExplorerModel getModel() {
        return _myModel;
    }
    
    public void createFolder(String name, String parentPath) {
                    
            File newFile = new File(parentPath + "/" + name);

            if (!"".equals(name) && newFile.mkdir()) {
                System.out.println("creation ok");
            }
    }
    
    public void deleteFolder(String path) {
        File newFile = new File(path + "/");
        if (newFile.delete()) {
            System.out.println("Suppression ok");
        }
    }
    
    public void renameFolder(File actual, String newName) {
        File renamedFile = new File(actual.getParent() + "/" + newName);
        if (actual.renameTo(renamedFile.getAbsoluteFile())) {
            System.out.println("Renommage ok");
        }
    }
    
    public ArrayList<FileModel> getFileModels(String path) {
        ArrayList<FileModel> fileModels = new ArrayList<FileModel>();
        File[] myFiles = new File(path).listFiles();
        
        if (myFiles != null) {
           
            for (File f : myFiles) {
                fileModels.add(new FileModel(f));
            }
        }
        return fileModels;
    }
}
